using System.Dynamic;
using UnityEngine;

public class TurretUpgrade : MonoBehaviour
{
    [SerializeField, Header("升级初始费用")]
    private int upgradeInitialCost;
    [SerializeField, Header("升级费用递增值")]
    private int upgradeCostIncremental;
    [SerializeField, Header("伤害递增值")]
    private float damageIncremental;
    [SerializeField, Header("攻击间隔递减值")]

    private float delayReduce;

    private TurretProjectile _turretProjectile;

    public int UpgradeCost { get; set; }
    public int Level { get; set; }

    [Header("出售折扣")]
    [Range(0, 1)]
    [SerializeField] private float sellPert;
    public float SellPerc { get; set; }

    private void Start()
    {
        _turretProjectile = GetComponent<TurretProjectile>(); // 获取 TurretProjectile 组件
        UpgradeCost = upgradeInitialCost;
        Level = 1;
        SellPerc = sellPert;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.D))
        {
            UpgradeTurret(); // 升级炮塔
        }
    }

    //计算出售价格
    public int GetsellValue()
    {
        int sellValue = Mathf.RoundToInt(UpgradeCost * SellPerc);
        return sellValue;
    }


    public void UpgradeTurret()
    {
        if (CurrencySystem.Instance.TotalCoins >= UpgradeCost)
        {
            _turretProjectile.Damge += damageIncremental; // 增加炮塔的伤害值
            _turretProjectile.DelayPerShot -= delayReduce;// 减少炮塔的攻击间隔
            UpdateUpgrade();
        }
    }

    private void UpdateUpgrade()
    {
        CurrencySystem.Instance.RemoveCoins(UpgradeCost);//扣除金额
        UpgradeCost += upgradeCostIncremental;//加下次升级金额
        Level++;
    }

}

